using System;
using System.Drawing;
using System.Globalization;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;

namespace HWEngine
{
	[Serializable]
	public class MutationVertPosition : ILetterMutation
	{
		private Random rand = new Random();

		[XmlAttribute("D")]
		public float D;

		[XmlAttribute("Enabled")]
		public bool Enabled;

		public MutationVertPosition()
		{
		}

		public MutationVertPosition(float d)
		{
			D = d;
			Enabled = true;
		}

		public void Mutate(Letter s)
		{
			if (!Enabled) return;
			float shift = (float) ((rand.NextDouble() - 0.5)*D);
			foreach (Line l in s.Lines)
			{
				for (int i = 0; i < l.Points.Count; i++)
				{
					PointF p = l.Points[i];
					p.Y += shift;
					l.Points[i] = p;
				}
			}
		}

		public void LoadFromXml(XPathNavigator xml)
		{
			D = (float) Convert.ToDouble(xml.GetAttribute("D", String.Empty), CultureInfo.InvariantCulture);
			Enabled = Convert.ToBoolean(xml.GetAttribute("Enabled", String.Empty), CultureInfo.InvariantCulture);
		}

		public XmlElement SaveToXml(XmlDocument parentDoc)
		{
			XmlElement el = parentDoc.CreateElement("mutationVertPosition");
			el.SetAttribute("D", D.ToString(CultureInfo.InvariantCulture));
			el.SetAttribute("Enabled", Enabled.ToString());
			return el;
		}
	}
}